home *** CD-ROM | disk | FTP | other *** search
/ PC World Komputer 2010 April / PCWorld0410.iso / hity wydania / Ubuntu 9.10 PL / karmelkowy-koliberek-desktop-9.10-i386-PL.iso / casper / filesystem.squashfs / usr / share / pyshared / DistUpgrade / DistUpgradeConfigParser.py < prev    next >
Text File  |  2009-11-02  |  2KB  |  54 lines

  1. from ConfigParser import ConfigParser, NoOptionError, NoSectionError
  2. import subprocess
  3. import os.path
  4. import logging
  5. import glob
  6.  
  7. CONFIG_OVERRIDE_DIR =  "/etc/update-manager/release-upgrades.d"
  8.  
  9. class DistUpgradeConfig(ConfigParser):
  10.     def __init__(self, datadir, name="DistUpgrade.cfg"):
  11.         ConfigParser.__init__(self)
  12.         # we support a config overwrite, if DistUpgrade.cfg.dapper exists
  13.         # and the user runs dapper, that one will be used
  14.         from_release = subprocess.Popen(["lsb_release","-c","-s"],
  15.                                         stdout=subprocess.PIPE).communicate()[0].strip()
  16.         self.datadir=datadir
  17.         if os.path.exists(name+"."+from_release):
  18.             name = name+"."+from_release
  19.         maincfg = os.path.join(datadir,name)
  20.         override_dir = CONFIG_OVERRIDE_DIR
  21.         self.config_files = [maincfg]
  22.         for cfg in glob.glob(override_dir+"/*.cfg"):
  23.             self.config_files.append(cfg)
  24.         self.read(self.config_files)
  25.     def getWithDefault(self, section, option, default):
  26.         try:
  27.             return self.get(section, option, raw=True)
  28.         except (NoSectionError, NoOptionError),e:
  29.             return default
  30.     def getlist(self, section, option):
  31.         try:
  32.             tmp = self.get(section, option)
  33.         except (NoSectionError,NoOptionError),e:
  34.             return []
  35.         items = [x.strip() for x in tmp.split(",")]
  36.         return items
  37.     def getListFromFile(self, section, option):
  38.         try:
  39.             filename = self.get(section, option)
  40.         except NoOptionError:
  41.             return []
  42.         p = os.path.join(self.datadir,filename)
  43.         if not os.path.exists(p):
  44.             logging.error("getListFromFile: no '%s' found" % p)
  45.         items = [x.strip() for x in open(p)]
  46.         return filter(lambda s: not s.startswith("#") and not s == "", items)
  47.  
  48.  
  49. if __name__ == "__main__":
  50.     c = DistUpgradeConfig()
  51.     print c.getlist("Distro","MetaPkgs")
  52.     print c.getlist("Distro","ForcedPurges")
  53.     print c.getListFromFile("Sources","ValidMirrors")
  54.